package com.citygis.transferdata.api.source1.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.citygis.transferdata.api.source1.dao.MasterMapper;
import com.citygis.transferdata.api.source1.service.MasterService;
import com.citygis.transferdata.api.source2.dao.SecondMapper;
import com.citygis.transferdata.api.source2.entity.STcTaskMedia;
import com.citygis.transferdata.api.source2.entity.TcDetailMedia;
import com.citygis.transferdata.api.source2.entity.TcXcJgwbk;
import com.citygis.transferdata.api.source2.entity.WlTaskMedia;
import com.citygis.transferdata.api.source2.service.SecondService;
import com.citygis.transferdata.config.resultBody.ResultBody;
import com.citygis.transferdata.tools.DateUtil;
import com.citygis.transferdata.tools.InitSysLoad;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: MasterServiceImpl 
 * @Description: TODO
 * @author: liuzg
 * @Date: 2021/8/31 10:00
 * @Version: v1.0
 */
@Slf4j
@Service
public class MasterServiceImpl implements MasterService {

    @Resource
    private MasterMapper mapper;

    @Resource
    private SecondService secondService;

    @Resource
    private SecondMapper secondMapper;

    @Override
    public List<JSONObject> getList() {

        return mapper.getList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override // 开始处理数据
    public ResultBody processData() {
        String beginDate = DateUtil.getDate(0);
        log.info("开始更新数据、当前时间为： " + beginDate);
        String recodeTime = this.obtainLastTime(1);
        log.info("上次同步时间： " + recodeTime);

        List<String> tableList = Arrays.asList(InitSysLoad.EXISTTIME.split(","));
        this.DataPush(tableList, recodeTime);

        secondMapper.updateThisSyncTime(1);
        String timeCost = "更新数据成功、用时： " + DateUtil.getTtimeDifference(DateUtil.getDate(0), beginDate);
        log.info(timeCost);
        return ResultBody.success(timeCost);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override // 同步媒体
    public ResultBody processMediaData() {
        String beginDate = DateUtil.getDate(0);
        log.info("开始更新媒体数据、当前时间为： " + beginDate);

        String recodeTime = this.obtainLastTime(2);
        log.info("上次同步时间： " + recodeTime);

        List<String> tableList = Arrays.asList(InitSysLoad.MEDIATABLES.split(","));
        this.pushMedia(tableList, recodeTime);

        secondMapper.updateThisSyncTime(2);
        String timeCost = "更新媒体数据成功、用时： " + DateUtil.getTtimeDifference(DateUtil.getDate(0), beginDate);
        log.info(timeCost);
        return ResultBody.success(timeCost);
    }

    // 获取媒体数据
    private void pushMedia(List<String> tables, String recodeTime) {
        for (String table : tables) {
            this.obtainData(table, recodeTime);
        }
    }

    // 获取媒体数据
    private void obtainData(String table, String recodeTime) {
        if ("S_TC_TASKMEDIA".equals(table)) {
            List<STcTaskMedia> list = mapper.selectOne(recodeTime);
            secondService.pushMedia(list, null, null, null, table);
        } else if ("TC_DETAILMEDIA".equals(table)) {
            List<TcDetailMedia> list = mapper.selectTwo(recodeTime);
            secondService.pushMedia(null, list, null, null, table);
        } else if ("WL_TASKMEDIA".equals(table)) {
            List<WlTaskMedia> list = mapper.selectThree(recodeTime);
            secondService.pushMedia(null, null, list, null, table);
        } else if ("TC_XCJGWBK".equals(table)) { // 目前荒废、按照普通表同步
            List<TcXcJgwbk> list = mapper.selectFour(recodeTime);
            secondService.pushMedia(null, null, null, list, table);
        }
    }

    // 获取需要同步的数据
    private void DataPush(List<String> tables, String recodeTime) {
        String sql = "";
        for (String table : tables) {
            sql = this.obtainQuerySql(table, recodeTime);
            List<Map<String, Object>> tableData = mapper.getTableData(sql);
            if (null == tableData || tableData.size() == 0) {
                log.info("本次查询表：" + table + "、结果集为：" + tableData.size());
                continue;
            }
            secondService.DataPush(tableData, table);
        }
    }

    /**
     * 获取本次需要同步的查询语句
     *
     * @param tableName
     * @param recodeTime
     * @return
     */
    public String obtainQuerySql(String tableName, String recodeTime) {
        List<String> existData = Arrays.asList(InitSysLoad.EXISTDATE.split(","));
        List<String> allPush = Arrays.asList(InitSysLoad.ALLPUSH.split(","));
        String sqlStrStart = "SELECT * " + " FROM " + tableName + " WHERE ";
        String sqlStrEnd = "BETWEEN TO_DATE(" + "'" + recodeTime + "'" + ",'yyyy-MM-dd hh24:mi:ss') AND SYSDATE";
        if (existData.contains(tableName)) {
            sqlStrStart += " UPDATEDATE " + sqlStrEnd;
        } else if (allPush.contains(tableName)) {
            sqlStrStart = "SELECT * " + " FROM " + tableName;
        } else if ("S_T_KEEPERSTRACK".equals(tableName)) {
            sqlStrStart += " INSERTTIME " + sqlStrEnd;
        } else {
            sqlStrStart += " UPDATETIME " + sqlStrEnd;
        }
        log.info("本次查询语句：--> " + sqlStrStart);
        return sqlStrStart;
    }


    /**
     * 获取上次同步时间
     *
     * @param id
     * @return
     */
    public String obtainLastTime(int id) {
        String lastSyncTime = secondMapper.getLastSyncTime(id);
        if (lastSyncTime.contains(".")) {
            lastSyncTime = lastSyncTime.substring(0, 19);
        }
        return lastSyncTime;
    }


}
